package com.jiutong.meipengdai.ui.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.chechezhi.ui.guide.FragmentTabAdapter;
import com.chechezhi.ui.guide.SinglePage;
import com.gyf.barlibrary.ImmersionBar;
import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.AppManager;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.ui.fragments.EntryFragment;
import com.jiutong.meipengdai.utils.SpConfig;
import com.jiutong.meipengdai.views.GuideView;

import java.util.ArrayList;
import java.util.List;

/**
 * com.best.bestedu.activity
 * create for 引导页  引导客户进入APP
 * Created by liuqi on 2016/7/25.
 */
public class GuideActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List guideContent = this.buildGuideContent();
        if(guideContent != null) {
            FrameLayout container = new FrameLayout(this);
            ViewPager pager = new ViewPager(this);
            pager.setId(this.getPagerId());
            container.addView(pager, new ViewGroup.LayoutParams(-1, -1));
            setContentView(container);

            AppManager.getAppManager().addActivity(this);

            //激活沉浸式状态栏  第三方库ImmersionBar
            ImmersionBar.with(this)
                    .statusBarDarkFont(true, 0.2f)
                    .init();

            FragmentTabAdapter adapter = new FragmentTabAdapter(this, guideContent);
            pager.setAdapter(adapter);
            GuideView guideView = new GuideView(this, guideContent, this.drawDot(), this.dotDefault(), this.dotSelected());
            pager.setOnPageChangeListener(guideView);
            container.addView(guideView, new android.widget.FrameLayout.LayoutParams(-1, -1));
        }
    }

    public List<SinglePage> buildGuideContent() {
        List<SinglePage> guideContent = new ArrayList<SinglePage>();

        SinglePage page01 = new SinglePage();
        page01.mBackground = getResources().getDrawable(R.mipmap.guide_page1);
        guideContent.add(page01);

        SinglePage page02 = new SinglePage();
        page02.mBackground = getResources().getDrawable(R.mipmap.guide_page2);
        guideContent.add(page02);

        SinglePage page03 = new SinglePage();
        page03.mCustomFragment = new EntryFragment();
        guideContent.add(page03);

        return guideContent;
    }

    public boolean drawDot() {
        return true;
    }

    public Bitmap dotDefault() {
        return BitmapFactory.decodeResource(getResources(),R.mipmap.gray_noselected_point);
    }

    public Bitmap dotSelected() {
        return BitmapFactory.decodeResource(getResources(),R.mipmap.orange_selected_point);
    }

    public int getPagerId() {
        return R.id.guide_container;
    }


    public void entryApp(){
        SpConfig.getInstance().putBool(Constants.IS_FIRST_USE_BOOLEAN, false);
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
